<!--论文隐匿名评审审批 审核专员-->
<template>
    <div class="con">
        <inner-breadcrumb v-model="pathKey" :path="path"></inner-breadcrumb>
        <div v-show="pathKey === 'main'">
            <ConditionCard
                shadow="never"
                body-style="padding: 24px 24px 6px 24px;"
                @search="onSubmit"
                @reset="reset('queryForm')"
            >
                <el-form
                    ref="queryForm"
                    label-suffix="："
                    :model="queryForm"
                    label-position="right"
                    label-width="120px"
                >
                    <el-row :gutter="24">
                        <el-col :span="8">
                            <el-form-item :label="$t('label.schoolTerm')">
                                <CalendarSelect v-model="queryForm.yearTerm" @load-done="loadDone"></CalendarSelect>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('label.college')">
                                <el-select
                                    v-model="queryForm.academyCode"
                                    style="width: 100%;"
                                    clearable
                                    filterable
                                    :placeholder="$t('common.text.all')"
                                    v-clearreadonly
                                >
                                    <el-option
                                        v-for="item in options.academyCode"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('label.major')">
                                <MyMajor
                                    :faculty="queryForm.academyCode"
                                    :filterable="true"
                                    :grade="queryForm.grade"
                                    v-model="queryForm.majorCode"
                                    :clearable="true"
                                    v-clearreadonly
                                ></MyMajor>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="24">
                        <el-col :span="8">
                            <el-form-item :label="$t('paperBlindTrial.stuTutorName')">
                                <el-select
                                    v-model="queryForm.stuTutorNo"
                                    :remote-method="selectUser"
                                    :placeholder="$t('preDefense.teacherNameTips')"
                                    filterable
                                    remote
                                    style="width: 100%;"
                                    v-clearreadonly
                                >
                                    <el-option
                                        v-for="item in users"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('paperBlindTrial.thesisTitle')">
                                <el-input
                                    v-model="queryForm.thesisTitle"
                                    :placeholder="$t('common.pla.pleaseInput')"
                                    :clearable="true"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('label.trainingLevel')">
                                <DictSelect
                                    v-model="queryForm.eduLevel"
                                    :data="options.eduLevel"
                                    :clearable="true"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="24">
                        <el-col :span="8">
                            <el-form-item :label="$t('label.trainingCategory')">
                                <DictSelect
                                    :filterable="true"
                                    v-model="queryForm.eduMode"
                                    :data="options.eduMode"
                                    :clearable="true"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('label.degreeCategory')">
                                <DictSelect
                                    v-model="queryForm.degreeType"
                                    :data="options.degreeType"
                                    :clearable="true"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('label.formLearning')">
                                <DictSelect
                                    v-model="queryForm.studyForm"
                                    :data="options.studyForm"
                                    :clearable="true"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="24">
                        <el-col :span="8">
                            <el-form-item :label="$t('paperBlindTrial.evaExpertCount')">
                                <DictSelect
                                    v-model="queryForm.evaExpertCount"
                                    :data="options.passCount"
                                    :clearable="true"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('paperBlindTrial.sendCount')">
                                <DictSelect
                                    v-model="queryForm.evaCount"
                                    :data="options.evaCount"
                                    :clearable="true"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('paperBlindTrial.preDefenseFlag')">
                                <DictSelect
                                    v-model="queryForm.preDefenseFlag"
                                    :data="options.preDefenseFlag"
                                    :clearable="true"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="24">
                        <el-col :span="8">
                            <el-form-item :label="$t('paperBlindTrial.sendStatus')">
                                <DictSelect
                                    v-model="queryForm.evaStatus"
                                    :data="options.evaStatus"
                                    :clearable="true"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :label="$t('courseSummary.aduitType')">
                                <DictSelect
                                    v-model="queryForm.status"
                                    :data="options.applyStatus"
                                    :clearable="true"
                                ></DictSelect>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </ConditionCard>
            <el-card shadow="never" body-style="padding: 24px;">
                <h1 class="condition-title">{{$t('paperBlindTrial.reviewApprovalTitle')}}</h1>
                <div class="edu-opertion-bar">
                    <div class="float-left">
                        <KeywordInput v-model="queryForm.keyword" :placeholder="$t('achievementApply.tableIplaceholder')" @enter="search"></KeywordInput>
                    </div>
                    <div class="float-right">
                        <el-button type="primary" @click="showSetDialog">{{$t('paperBlindTrial.specialSet')}}</el-button>
                        <el-button type="primary" @click="batchSendAudit">{{$t('paperBlindTrial.batchSend')}}</el-button>
                        <el-button type="primary" @click="batchDownload()">{{$t('paperBlindTrial.batchDownload')}}</el-button>
                        <el-button type="primary" @click="exportFunc">{{ $t('message.export') }}</el-button>
                    </div>
                </div>
                <edu-table ref="tables" :url="tableApi" @load-done="tableLoadDone" @selection-change="handleSelectionChange" :orderFixed="true">
                    <el-table-column :min-width="90" :label="$t('label.stuCode')" prop='stuNo' show-overflow-tooltip fixed="left">
                    </el-table-column>
                    <el-table-column :min-width="70" :label="$t('label.stuName')" prop='stuName' show-overflow-tooltip fixed="left">
                    </el-table-column>
                    <el-table-column
                        :min-width="item.width"
                        v-for="item in label"
                        :key="item.props"
                        :label="item.label"
                        :prop="item.props"
                        show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column
                        :min-width="90"
                        :label="$t('paperBlindTrial.preDefenseFlag')"
                        show-overflow-tooltip
                        :formatter="
                            row => {
                                return fieldChange(options.preDefenseFlag, row.preDefenseFlag)
                            }
                        "
                    ></el-table-column>
                    <el-table-column :min-width="155" prop="createTime" :label="$t('paperBlindTrial.applicantTime')" show-overflow-tooltip></el-table-column>
                    <el-table-column
                        :min-width="80"
                        :label="$t('paperBlindTrial.sendCount')"
                        prop="evaCount"
                        show-overflow-tooltip
                        :formatter="
                            row => {
                                return row.evaCount==0?'':row.evaCount
                            }
                        "
                    ></el-table-column>
                    <el-table-column :min-width="90" :label="$t('paperBlindTrial.evaExpertCount')" show-overflow-tooltip fixed="right">
                        <template slot-scope="scope">
                            <el-input
                                v-if="!scope.row.editState && lastState(scope.row)"
                                v-model="scope.row.evaExpertCount"
                                placeholder
                            ></el-input>
                            <label v-else>{{ scope.row.evaExpertCount }}</label>
                        </template>
                    </el-table-column>
                    <el-table-column :min-width="80" :label="$t('paperBlindTrial.sendStatus')" show-overflow-tooltip fixed="right">
                        <template slot-scope="scope">
                            <el-select
                                v-if="!scope.row.editState && lastState(scope.row)"
                                v-model="scope.row.evaStatus"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.evaStatus"
                                    :label="item.label"
                                    :value="item.value"
                                    :key="item.value"
                                ></el-option>
                            </el-select>
                            <label v-else>{{ fieldChange(options.evaStatus, scope.row.evaStatus) }}</label>
                        </template>
                    </el-table-column>
                    <el-table-column :min-width="100" :label="$t('courseSummary.aduitType')" show-overflow-tooltip fixed="right">
                        <template slot-scope="scope">
                            <el-tag
                                v-if="getType(scope.row.status)"
                                :type="getType(scope.row.status)"
                            >{{ fieldChange(options.applyStatus, scope.row.status) }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column
                        width="130px"
                        :label="$t('nonPublicCourseOpen.operation')"
                        fixed="right"
                    >
                        <template slot-scope="scope">
                            <div class="con_btns">
                                <el-button
                                    type="text"
                                    @click="audit('view', scope.row)"
                                >{{ $t('message.Look') }}</el-button>
                                <el-button type="text" v-if="scope.row.status === 'BLIND_SUBMIT'" @click="audit('audit', scope.row)">{{$t('common.btn.approve')}}</el-button>
                                <el-button type="text" @click="batchDownload(scope.row)">{{$t('paperBlindTrial.downLoadLabel')}}</el-button>
                                <span v-if="scope.row.status === 'PASS' && lastState(scope.row)">
                                  <el-button
                                      type="text"
                                      v-if="!scope.row.editState"
                                      @click="saveRow(scope.row)"
                                  >{{$t('common.btn.save')}}</el-button>
                                  <el-button type="text" v-if="scope.row.editState" @click="editRow(scope.row)">{{$t('common.btn.edit')}}</el-button>
                                </span>
                            </div>
                        </template>
                    </el-table-column>
                </edu-table>
            </el-card>
        </div>
        <!-- 申请审批 -->
        <paperAudit
            v-if="pathKey === 'paperAudit'"
            :editForm="false"
            :auditType="auditType"
            :row="objs"
            :resultState="true"
            @emit:back="backFunc"
        ></paperAudit>
        <!-- 特殊通过篇数设置弹框 -->
        <el-dialog
          :title="$t('paperBlindTrial.specialSet')"
          :visible.sync="dialogVisible"
          width="450px"
          @close="closeFunc">
          <el-form ref="specialForm" :model="specialForm" label-suffix=":" :rules="rules" label-width="90px">
            <el-form-item :label="$t('paperBlindTrial.needPassNum')" prop="needPassCount">
              <el-input v-model="specialForm.needPassCount" placeholder="" maxlength="10"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">{{$t('message.cancel')}}</el-button>
            <el-button type="primary" @click="addSpecialNum">{{$t('message.confirm')}}</el-button>
          </span>
        </el-dialog>
    </div>
</template>

<script>
import { adminAuditTableList, adminAuditUpadte, adminAuditBatchCheck,
adminAuditExport, adminAuditDownloadZip, specialSetting } from 'common/src/api/paperBlindTrial'
import MyMajor from 'common/src/views/administrator/undergraduateTutor/component/MyMajor'
import paperAudit from '../paperReviewApproval/paperAudit'
import commonMixins from 'common/src/views/practiceManagement/commonMixins'
import { queryDic } from 'common/src/api/dictionary'
import { findTeacherInfoList } from 'common/src/api/studentinfo'
export default {
    components: {
        MyMajor,
        paperAudit
    },
    mixins: [commonMixins],
    props: {},
    data() {
        return {
            tableApi: adminAuditTableList,
            objs: {},
            yearTerms: '',
            queryForm: {
                keyword: '',
                "yearTerm": "",
                "academyCode": "",
                "majorCode": "",
                "stuTutorNo": "",
                "thesisTitle": "",
                "eduLevel": "",
                "eduMode": "",
                "degreeType": "",
                "studyForm": "",
                "evaExpertCount": '',
                "evaCount": '',
                "preDefenseFlag": "",
                "evaStatus": "",
                "status": "BLIND_SUBMIT"
            },
            label: [
                // { label: this.$t('label.schoolTerm'), props: 'yearTermI18n', width: 180 },
                // { label: this.$t('label.stuCode'), props: 'stuNo', width: 100 },
                // { label: this.$t('label.stuName'), props: 'stuName', width: 120 },
                { label: this.$t('label.grade'), props: 'grade', width: 80 },
                { label: this.$t('label.college'), props: 'academyName', width: 150 },
                { label: this.$t('label.major'), props: 'majorName', width: 150 },
                { label: this.$t('achievementApply.thesisTitle'), props: 'thesisTitle', width: 130 },
                { label: this.$t('achievementApply.teacherName'), props: 'stuTutorName', width: 80 },
                { label: this.$t('label.trainingLevel'), props: 'eduLevelI18n', width: 80 },
                // { label: this.$t('label.trainingCategory'), props: 'eduModeI18n', width: 100 },
                { label: this.$t('label.degreeCategory'), props: 'degreeTypeI18n', width: 80 },
                { label: this.$t('label.formLearning'), props: 'studyFormI18n', width: 80 },
            ],
            options: {
                academyCode: [], // 学院
                applyStatus: [
                  { label: this.$t('paperBlindTrial.tutorSubmit'), value: 'TUTOR_SUBMIT', type: 'warning' },
                  { label: this.$t('paperBlindTrial.blindSubmit'), value: 'BLIND_SUBMIT', type: 'warning' },
                  { label: this.$t('paperBlindTrial.tutorPass'), value: 'TUTOR_PASS', type: 'success' },
                  { label: this.$t('paperBlindTrial.agreeLabel'), value: 'PASS', type: 'success' },
                  { label: this.$t('achievementApply.refuse'), value: 'REJECT', type: 'danger' },
                ],
                eduLevel: [], // 培养层次
                eduMode: [], // 培养类别
                degreeType: [], // 学位类型
                studyForm: [], // 学习形式
                majorCode: [],
                passCount: [],
                preDefenseFlag: [
                  { label: this.$t('message.yes'), value: 'Y', type: 'success' },
                  { label: this.$t('message.no'), value: 'N', type: 'danger' }
                ],
                evaStatus: [
                  { label: this.$t('paperBlindTrial.hasSend'), value: 'Y', type: 'success' },
                  { label: this.$t('paperBlindTrial.notSend'), value: 'N', type: 'warning' }
                ],
                evaCount: []
            },
            pathKey: 'main',
            path: {
                label: this.$t('paperBlindTrial.approvalLable'),
                key: 'main',
                disable: false,
                children: [{ label: this.$t('message.detail'), key: 'paperAudit', disable: true }]
            },
            auditType: '',
            users: [],
            selectionDatas: [], // 多选方法
            tableList: [],
            specialForm: {
              needPassCount: ''
            },
            rules: {
              needPassCount: [
                { required: true, validator: this.validateRate }
              ]
            },
            dialogVisible: false
        }
    },
    computed: {},
    created() {},
    mounted() {
        let param = {
            X_YX: 'academyCode', // 学院
            // X_PYCC: 'eduLevel', // 培养层次
            X_PYLB: 'eduMode', // 培养类别
            X_XWLX: 'degreeType', // 学位类型
            K_XXXS: 'studyForm', // 学习形式
            G_ZY: 'majorCode' // 专业:
        }
        this.initOptions(param)
        this.onSubmit()
        this.getEdulevel()
        for (let i = 1; i <= 50; i++) {
            let obj = { label: i, value: i, id: i }
            this.options.passCount.push(obj)
        }
        for (let i = 0; i <= 50; i++) {
            let obj = { label: i, value: i, id: i }
            this.options.evaCount.push(obj)
        }
    },
    methods: {
        getEdulevel() {
            // val: {  X_PYCC: 'trainingLevel',   G_ZY: 'profession'} // 专业
            const data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: ['X_PYCC']
            }
            return new Promise((resolve, reject) => {
                queryDic(data)
                    .then(res => {
                        if (res.code === 200) {
                            this._fill('eduLevel', res.data['X_PYCC'])
                            this.options.eduLevel = this.options.eduLevel.filter(
                                v => v.value === '4' || v.value === '6'
                            )
                        }
                        resolve(this.options)
                    })
                    .catch(err => {
                        reject(err)
                    })
            })
        },
        onSubmit() {
            // 筛选
            this.$refs.tables.pageNum = 1
            this.$refs.tables.search(this.queryForm)
        },
        // 表格数据加载完成后调用
        tableLoadDone (data) {
          this.tableList = _.cloneDeep(data);
          data.forEach(item => {
            item.editState = !item.evaStatus || item.evaStatus == 'N';
          });
        },
        search() {
            this.queryForm.keyword = this.queryForm.keyword.trim()
            this.onSubmit()
        },
        audit(type, row) {
            // 审批
            this.pathKey = 'paperAudit'
            this.objs = row
            this.auditType = type
        },
        reset() {
            // 重置
            for (const key in this.queryForm) {
                if (key !== 'yearTerm') {
                    this.queryForm[key] = ''
                }
            }
            this.queryForm.status = 'BLIND_SUBMIT';
            this.queryForm.yearTerm = this.yearTerms
            this.onSubmit()
        },
        loadDone() {
            // 页面加载时保存学期学年
            this.yearTerms = this.queryForm.yearTerm
        },
        // 审批完成返回方法
        backFunc() {
            this.pathKey = 'main'
            this.onSubmit()
        },
        getType(val) {
            let option = this.options.applyStatus.find(v => v.value == val)
            return option ? option.type : null
        },
        // 保存方法
        saveRow(row) {
            let evaExpertCount = row.evaExpertCount
            let intReg = /^[1-9][0-9]*$/ // 正整数
            if (!evaExpertCount || evaExpertCount === '') {
                return this.$message.error(this.$t('paperBlindTrial.evaExpertCountTips'))
            }
            if (!intReg.test(evaExpertCount)) {
                return this.$message.error(this.$t('paperBlindTrial.moreThenZero'));
            }
            let param = {
              "id": row.id,		//主键ID，填写详情里面的id
              "applyId": row.applyId,	//申请ID，填写详情里面的applyId
              "evaStatus": row.evaStatus,	//必填，送审状态，Y-送审，N-未送审
              "evaExpertCount": row.evaExpertCount 	//必填，送审专家数
            };
            adminAuditUpadte(param).then(res=>{
              if (res.code == 200) {
                this.$message.success(this.$t('common.tips.opSuccess'));
                this.onSubmit();
              }
            });
        },
        editRow(row) {
          row.editState = !row.editState;
        },
        //可用TeacherSelect通用组件替换
        selectUser(query) {
            if (!query) return
            const data = {
                condition: query,
                pageNum_: 1,
                pageSize_: 10
            }
            this.loading = true
            findTeacherInfoList(data)
                .then(res => {
                    this.loading = false
                    if (res.code == 200) {
                        this.users = this.transData(res.data.list, 'name', 'code')
                    } else throw res.code
                })
                .catch(error => {
                    this.loading = false
                    this.$message({
                        type: 'error',
                        message: '查询用户出错'
                    })
                })
        },
        transData(list, name, code) {
            return list.map((item, index, array) => {
                return {
                    label: item[name] + '(' + item[code] + ')',
                    value: item[code]
                }
            })
        },
        // 多选方法
        handleSelectionChange (val) {
          this.selectionDatas = val;
        },
        // 批量送审
        batchSendAudit() {
          if (this.selectionDatas.length === 0) {
            this.$message.error(this.$t('paperBlindTrial.sendTips'));
            return;
          }
          for (let item of this.selectionDatas) {
            let evaExpertCount = item.evaExpertCount
            let intReg = /^[1-9][0-9]*$/ // 正整数
            if (item.status !== 'PASS') {
              this.$message.error(this.$t('paperBlindTrial.sendBatchTips'));
              return;
            }
            if (item.evaStatus == 'Y') {
              this.$message.error(this.$t('paperBlindTrial.hasSendTips'));
              return;
            }
            // 校验送审专家数
            if (!evaExpertCount || evaExpertCount === '') {
              this.$message.error(this.$t('paperBlindTrial.evaExpertCountTips'))
              return;
            }
            if (!intReg.test(evaExpertCount)) {
              this.$message.error(this.$t('paperBlindTrial.moreThenZero'));
              return;
            }
          }
          this.$eduConfirm(this.$t('paperBlindTrial.batchSureTips'), this.$t('achievementApply.tipsTitle')).then(() => {
            this.sure();
          });
        },
        sure() {
          let param = [];
          for (let item of this.selectionDatas) {
            let paramObj = { id: item.id, applyId: item.applyId, evaStatus: 'Y', evaExpertCount: item.evaExpertCount };
            param.push(paramObj);
          }
          adminAuditBatchCheck(param).then(res=> {
            if (res.code === 200) {
              this.$message.success(this.$t('common.tips.opSuccess'));
              this.onSubmit();
            }
          });
        },
        // 批量下载
        batchDownload(singleRow = {}) {
          let downloadParam = Object.keys(singleRow).length > 0 ? [singleRow] : this.selectionDatas;
          // if (downloadParam.length === 0) {
          //   this.$message.error(this.$t('paperBlindTrial.downLoadTips'));
          //   return;
          // }
          let param = _.cloneDeep(this.queryForm);
          param.applyIds = downloadParam.map(v=>v.applyId);
          adminAuditDownloadZip(param);
        },
        // 导出方法
        exportFunc() {
          let queryForm = Object.assign({}, this.queryForm);
          let rows = this.selectionDatas;
          let param = {
            exportType: rows.length > 0 ? 1 : 0,
            rows: rows,
            exportCondition: queryForm
          };
          adminAuditExport(param);
        },
        // 上次记录数据送审状态
        lastState(row) {
          return this.tableList.filter(v=>v.applyId===row.applyId)[0].evaStatus !== 'Y'
        },
        // 展示特殊设置通过数弹框方法
        showSetDialog() {
          if (this.selectionDatas.length === 0) {
            this.$message.error(this.$t('paperBlindTrial.specialTips'));
            return;
          }
          for (let item of this.selectionDatas) {
            if (item.finalResult === 'COMPLETE')  {
              this.$message.error(this.$t('paperBlindTrial.finishTips'));
              return;
            }
          }
          this.dialogVisible = true;
        },
        // 添加特殊设置确定方法
        addSpecialNum() {
          this.$refs.specialForm.validate(valid =>{
            if (valid) {
              let param = {
                stuNos: this.selectionDatas.map(v=> v.stuNo),
                needPassCount: this.specialForm.needPassCount
              };
              specialSetting(param).then(res=>{
                if (res.code === 200) {
                  this.dialogVisible = false;
                  this.$message.success(this.$t('paperBlindTrial.setSuccess'));
                }
              });
            }
          });
        },
        // 关闭弹框方法
        closeFunc() {
          this.$refs.specialForm.resetFields();
        },
        // 校验输入的抽取比例值
        validateRate (rule, value, callback) {
          let reng = /^[1-9][0-9]*$/ // 正整数
          if (!value || value.length === 0) {
            return callback(new Error(this.$t('inputPlaceholder')));
          }
          if (!reng.test(value)) {
            return callback(new Error(this.$t('paperBlindTrial.moreThenZero')));
          }
          return callback();
        },
    }
}
</script>

<style scoped lang="scss"></style>
